package com.itheima.a11threadsafe3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyRunnable implements Runnable {
    
    int ticket = 0;

    //创建锁对象
    Lock lock=new ReentrantLock();
    
    @Override
    public void run() {
        while (true) {
            //加锁
            lock.lock();
            try {
                if (ticket == 100) {
                    break;
                }else {
                    Thread.sleep(10);
                    ticket++;
                    System.out.println(Thread.currentThread().getName() + "正在卖第" + ticket + "张票");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                //释放锁
                lock.unlock();
            }

        }
    }
}
